package com.globaldom.web.Civics;

import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.CivResource;
import com.globaldom.utils.PMF;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/civTime")
public class CivilianGetController {
	public CivilianGetController() {
	}

	@RequestMapping(value = "/update", method = RequestMethod.GET)
	public @ResponseBody
	long getCivilian(@RequestParam int resType, HttpSession session, HttpServletRequest request) {
		String personID = SessionManager.getID(request, session);
		if (null == personID) {
			return 0;
		}
		PersistenceManager pmf = PMF.get().getPersistenceManager();

		try {

			CivResource cv = pmf.getObjectById(CivResource.class, personID + "/" + resType);
			cv.setResourceUpdated(new Date());

			return cv.getResAvailable();
		} finally {
			pmf.close();
		}

	}

}
